/**
 * 
 */
package acceptanceTests.writingAmessage;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.Socket;
import server.ForumServer;
import server.WSEPHandler;
import domain.items.Post;


//
/**
 * @author Administrator
 *
 */
public class WritingAmessageImp implements WritingAmessageInt {

	private boolean _implementedGetLegalTopicForNewMessage  = true;
	private boolean _implementedGetMessageTopic  = true;
	private boolean _implementedGetOwnerName  = true;
	private boolean _implementedWriteNewMessageReturnsMessage  = true;
	private boolean _implementedDeleteMessage  = true;
	
	
	
	private ForumServer _server;
	private WSEPHandler _handler;
	
	public WritingAmessageImp(ForumServer server){
		this._server = server;
		try{
			Socket s= new Socket("localhost", getPort());
			_handler= new WSEPHandler(s,server);
		}
		catch(IOException e){
		}
	}
	
	private static int getPort() {
		String str="0";
		try {
	        BufferedReader in = new BufferedReader(new FileReader("port.txt"));
	        str = in.readLine();
	        in.close();
	    } catch (IOException e) {
	    }
		return Integer.parseInt(str);
	}



	/* (non-Javadoc)
	 * @see acceptanceTests.writingAmessage.WritingAmessageInt#getLegalTopicForNewMessage(java.lang.String)
	 */

	public Object getLegalTopicForNewMessage(String userName) {
		// TODO Auto-generated method stub
		_handler.getRootTopic();
		_handler.setCurrentlyViewedItem(_server.getDataRepository().getRootTopic());
		_handler.getItem(1);

		return _handler.get_currentlyViewedItem().getName();

	}

	/* (non-Javadoc)
	 * @see acceptanceTests.writingAmessage.WritingAmessageInt#getMessageTopic(java.lang.Object)
	 */

	public Object getMessageTopic(Object message) {
		Post post= (Post)message;
		return post.getFather();

	}


	/* (non-Javadoc)
	 * @see acceptanceTests.writingAmessage.WritingAmessageInt#getOwnerName(java.lang.Object)
	 */

	public String getOwnerName(Object message) {
		Post p=(Post)message;
		return p.getOwner();

	}


	public Object writeNewMessageReturnsMessage(String subject, String body,
			Object topic, String userName) {
		Post p= new Post(_handler.get_currentlyViewedItem(),userName, subject, body);
		_handler.addThread(subject, body, userName);
		return p;

	}

	
	public boolean deleteMessage(Object message) {

		_handler.deletePost();
		return true;
	}


	public boolean isImplementedGetLegalTopicForNewMessage() {
		return _implementedGetLegalTopicForNewMessage;
	}

	public void setImplementedGetLegalTopicForNewMessage(
			boolean getLegalTopicForNewMessage) {
		_implementedGetLegalTopicForNewMessage = getLegalTopicForNewMessage;
	}

	public boolean isImplementedGetMessageTopic() {
		return _implementedGetMessageTopic;
	}

	public void setImplementedGetMessageTopic(boolean getMessageTopic) {
		_implementedGetMessageTopic = getMessageTopic;
	}

	
	public boolean isImplementedGetOwnerName() {
		return _implementedGetOwnerName;
	}

	public void setImplementedGetOwnerName(boolean getOwnerName) {
		_implementedGetOwnerName = getOwnerName;
	}

	

	public boolean isImplementedWriteNewMessageReturnsMessage() {
		return _implementedWriteNewMessageReturnsMessage;
	}

	public void setImplementedWriteNewMessageReturnsMessage(
			boolean writeNewMessageReturnsMessage) {
		_implementedWriteNewMessageReturnsMessage = writeNewMessageReturnsMessage;
	}

	public boolean is_implementedDeleteMessage() {
		return _implementedDeleteMessage;
	}

	public void set_implementedDeleteMessage(boolean deleteMessage) {
		_implementedDeleteMessage = deleteMessage;
	}


}
